import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:speed_app/common/index.dart';

/// 登录输入框
class LoginInputWidget extends StatelessWidget {
  /// 标题
  final String? title;
  /// 占位文本
  final String? placeholder;
  /// 子组件
  final Widget? subWidget;
  /// 左侧组件
  final Widget? leftWidget;
  /// 右侧侧组件
  final Widget? rightWidget;
  /// 圆角
  final double? borderRadius;
  /// 标题居左右边距
  final double? horizontalTitle;
  /// 输入框居左右边距
  final double? horizontal;
  /// 输入文本
  final TextEditingController? inputController;
  /// 输入回车按钮样式
  TextInputAction? textInputAction;
  /// 输入键盘样式
  final TextInputType? keyboardType;
  /// 是否为密文输入
  bool? obscureText;
  /// 是否允许输入
  bool? enabled;

  LoginInputWidget({
    Key? key,
    this.title,
    this.placeholder,
    this.borderRadius = 25,
    this.horizontal = 0,
    this.horizontalTitle = 15,
    this.subWidget,
    this.leftWidget,
    this.rightWidget,
    this.inputController,
    this.textInputAction,
    this.keyboardType = TextInputType.text,
    this.obscureText = false,
    this.enabled = true,
  }) : super(key: key);

  /// 输入文本框
  Widget _inputWidget(BuildContext context){
    return Container(
      height: (borderRadius??25)*2,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(borderRadius??25), // 圆角半径
        border: Border.all(
          color: Colors.black, // 边框颜色
          width: 1.2, // 边框宽度
        ),
      ),
      child: <Widget>[
        // 左侧视图
        if (leftWidget != null)
          leftWidget!,
        // 输入框
        TextField(
          obscureText: obscureText??false,// 设置为 true 显示为密文
          controller: inputController,
          autocorrect: true,
          enabled: enabled, // 禁用输入
          keyboardType: TextInputType.text,
          textInputAction: textInputAction,
          style: TextStyle(
            fontSize: 15,
            fontWeight: FontWeight.bold,
            color: AppColors.blackContent,
          ),
          decoration: InputDecoration(
            contentPadding: const EdgeInsets.all(15.0),
            hintText: (enabled==true?'请输入':'请选择')+placeholder.placeholder(title),
            hintStyle: TextStyle(color: ExColor.hexColor('#A2B5CD'),), // 占位文本颜色
            border: const OutlineInputBorder(borderSide: BorderSide.none,),
          ),
          autofocus: false,
          onEditingComplete: () {
            /// 收起键盘
            FocusScope.of(context).unfocus();
          },
        ).expanded(),
        // 右侧视图
        if (rightWidget != null)
          rightWidget!,
      ].toRow(),
    ).paddingSymmetric(horizontal: horizontal!);
  }

  @override
  Widget build(BuildContext context) {
    return <Widget>[
      // 标题
      if (title != null && title!.isNotEmpty)
        Padding(
          padding: EdgeInsets.only(left: horizontalTitle!, right: horizontalTitle!, bottom: 5),
          child: Text(title!,
            style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      // 子组件
      if (subWidget != null)
        subWidget!,
      // 输入文本框
      _inputWidget(context),
    ].toColumn(crossAxisAlignment: CrossAxisAlignment.start);
  }

}